﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Clandestine
{
    public class MessageWindow : ResizableBG
    {
        public readonly string Message = string.Empty;
        public TextSprite MessageSprite;
        public Padding Padding = new Padding(3, 5, 5, 5);

        public MessageWindow(string message, Point position)
            : this(message)
        {
            this.Position = position;
        }
        public MessageWindow(string message)
            : base()
        {
            this.Message = message;
            MessageSprite = Fonts.Default2.DrawText(message);
            MessageSprite.Layer = this.Layer + 0.001f;
            this.Visible = false;
            MessageSprite.Visible = false;

            this.Resize();
            this.Position = new Point((Graphics.ScreenWidth / 2) - (this.Width / 2), (Graphics.ScreenHeight / 2) - (this.Height / 2));
        }

        public void ShowModally()
        {
            bool initialPaused = WorldMap.Paused;
            WorldMap.Paused = true;

            MessageSprite.Position = new Point(this.Position.X + Padding.Left, this.Position.Y + Padding.Top);
            this.Visible = true;
            MessageSprite.Visible = true;

            // Wait for select key to be pressed
            bool selection = false;
            KeyEventDelegate onKeyDown = new KeyEventDelegate(delegate(Key k)
            {
                if (k == Key.Select)
                    selection = true;
            });
            Keyboard.KeyDown += onKeyDown;
            while (!selection)
                System.Threading.Thread.Sleep(100);
            Keyboard.KeyDown -= onKeyDown;

            this.Visible = false;
            MessageSprite.Visible = false;
            this.Dispose();
            WorldMap.Paused = initialPaused;
        }

        public void Resize()
        {
            int width = Padding.Left + Padding.Right + MessageSprite.Width;
            int height = Padding.Top + Padding.Bottom + MessageSprite.Height + 2;
            Resize(new Size(width, height));
        }

        public override void Dispose()
        {
            MessageSprite.Dispose();
            base.Dispose();
        }
    }
}
